<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>32.3. symtable — Access to the compiler’s symbol tables &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="32. Python Language Services" href="language.html" />
    <link rel="next" title="32.4. symbol — Constants used with Python parse trees" href="symbol.html" />
    <link rel="prev" title="32.2. Abstract Syntax Trees" href="ast.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="symbol.html" title="32.4. symbol — Constants used with Python parse trees"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="ast.html" title="32.2. Abstract Syntax Trees"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="language.html" accesskey="U">32. Python Language Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-symtable">
<h1>32.3. <tt class="xref docutils literal"><span class="pre">symtable</span></tt> &#8212; Access to the compiler&#8217;s symbol tables<a class="headerlink" href="#module-symtable" title="Permalink to this headline">¶</a></h1>
<p>Symbol tables are generated by the compiler from AST just before bytecode is
generated.  The symbol table is responsible for calculating the scope of every
identifier in the code.  <tt class="xref docutils literal"><span class="pre">symtable</span></tt> provides an interface to examine these
tables.</p>
<div class="section" id="generating-symbol-tables">
<h2>32.3.1. Generating Symbol Tables<a class="headerlink" href="#generating-symbol-tables" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="symtable.symtable">
<tt class="descclassname">symtable.</tt><tt class="descname">symtable</tt><big>(</big><em>code</em>, <em>filename</em>, <em>compile_type</em><big>)</big><a class="headerlink" href="#symtable.symtable" title="Permalink to this definition">¶</a></dt>
<dd>Return the toplevel <a title="symtable.SymbolTable" class="reference internal" href="#symtable.SymbolTable"><tt class="xref docutils literal"><span class="pre">SymbolTable</span></tt></a> for the Python source <em>code</em>.
<em>filename</em> is the name of the file containing the code.  <em>compile_type</em> is
like the <em>mode</em> argument to <a title="compile" class="reference external" href="functions.html#compile"><tt class="xref docutils literal"><span class="pre">compile()</span></tt></a>.</dd></dl>

</div>
<div class="section" id="examining-symbol-tables">
<h2>32.3.2. Examining Symbol Tables<a class="headerlink" href="#examining-symbol-tables" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="symtable.SymbolTable">
<em class="property">
class </em><tt class="descclassname">symtable.</tt><tt class="descname">SymbolTable</tt><a class="headerlink" href="#symtable.SymbolTable" title="Permalink to this definition">¶</a></dt>
<dd><p>A namespace table for a block.  The constructor is not public.</p>
<dl class="method">
<dt id="symtable.SymbolTable.get_type">
<tt class="descname">get_type</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_type" title="Permalink to this definition">¶</a></dt>
<dd>Return the type of the symbol table.  Possible values are <tt class="docutils literal"><span class="pre">'class'</span></tt>,
<tt class="docutils literal"><span class="pre">'module'</span></tt>, and <tt class="docutils literal"><span class="pre">'function'</span></tt>.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.get_id">
<tt class="descname">get_id</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_id" title="Permalink to this definition">¶</a></dt>
<dd>Return the table&#8217;s identifier.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.get_name">
<tt class="descname">get_name</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_name" title="Permalink to this definition">¶</a></dt>
<dd>Return the table&#8217;s name.  This is the name of the class if the table is
for a class, the name of the function if the table is for a function, or
<tt class="docutils literal"><span class="pre">'top'</span></tt> if the table is global (<a title="symtable.SymbolTable.get_type" class="reference internal" href="#symtable.SymbolTable.get_type"><tt class="xref docutils literal"><span class="pre">get_type()</span></tt></a> returns <tt class="docutils literal"><span class="pre">'module'</span></tt>).</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.get_lineno">
<tt class="descname">get_lineno</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_lineno" title="Permalink to this definition">¶</a></dt>
<dd>Return the number of the first line in the block this table represents.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.is_optimized">
<tt class="descname">is_optimized</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.is_optimized" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the locals in this table can be optimized.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.is_nested">
<tt class="descname">is_nested</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.is_nested" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the block is a nested class or function.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.has_children">
<tt class="descname">has_children</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.has_children" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the block has nested namespaces within it.  These can
be obtained with <a title="symtable.SymbolTable.get_children" class="reference internal" href="#symtable.SymbolTable.get_children"><tt class="xref docutils literal"><span class="pre">get_children()</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.has_exec">
<tt class="descname">has_exec</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.has_exec" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the block uses <tt class="docutils literal"><span class="pre">exec</span></tt>.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.has_import_start">
<tt class="descname">has_import_start</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.has_import_start" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the block uses a starred from-import.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.get_identifiers">
<tt class="descname">get_identifiers</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_identifiers" title="Permalink to this definition">¶</a></dt>
<dd>Return a list of names of symbols in this table.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.lookup">
<tt class="descname">lookup</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#symtable.SymbolTable.lookup" title="Permalink to this definition">¶</a></dt>
<dd>Lookup <em>name</em> in the table and return a <a title="symtable.Symbol" class="reference internal" href="#symtable.Symbol"><tt class="xref docutils literal"><span class="pre">Symbol</span></tt></a> instance.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.get_symbols">
<tt class="descname">get_symbols</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_symbols" title="Permalink to this definition">¶</a></dt>
<dd>Return a list of <a title="symtable.Symbol" class="reference internal" href="#symtable.Symbol"><tt class="xref docutils literal"><span class="pre">Symbol</span></tt></a> instances for names in the table.</dd></dl>

<dl class="method">
<dt id="symtable.SymbolTable.get_children">
<tt class="descname">get_children</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_children" title="Permalink to this definition">¶</a></dt>
<dd>Return a list of the nested symbol tables.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="symtable.Function">
<em class="property">
class </em><tt class="descclassname">symtable.</tt><tt class="descname">Function</tt><a class="headerlink" href="#symtable.Function" title="Permalink to this definition">¶</a></dt>
<dd><p>A namespace for a function or method.  This class inherits
<a title="symtable.SymbolTable" class="reference internal" href="#symtable.SymbolTable"><tt class="xref docutils literal"><span class="pre">SymbolTable</span></tt></a>.</p>
<dl class="method">
<dt id="symtable.Function.get_parameters">
<tt class="descname">get_parameters</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Function.get_parameters" title="Permalink to this definition">¶</a></dt>
<dd>Return a tuple containing names of parameters to this function.</dd></dl>

<dl class="method">
<dt id="symtable.Function.get_locals">
<tt class="descname">get_locals</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Function.get_locals" title="Permalink to this definition">¶</a></dt>
<dd>Return a tuple containing names of locals in this function.</dd></dl>

<dl class="method">
<dt id="symtable.Function.get_globals">
<tt class="descname">get_globals</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Function.get_globals" title="Permalink to this definition">¶</a></dt>
<dd>Return a tuple containing names of globals in this function.</dd></dl>

<dl class="method">
<dt id="symtable.Function.get_frees">
<tt class="descname">get_frees</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Function.get_frees" title="Permalink to this definition">¶</a></dt>
<dd>Return a tuple containing names of free variables in this function.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="symtable.Class">
<em class="property">
class </em><tt class="descclassname">symtable.</tt><tt class="descname">Class</tt><a class="headerlink" href="#symtable.Class" title="Permalink to this definition">¶</a></dt>
<dd><p>A namespace of a class.  This class inherits <a title="symtable.SymbolTable" class="reference internal" href="#symtable.SymbolTable"><tt class="xref docutils literal"><span class="pre">SymbolTable</span></tt></a>.</p>
<dl class="method">
<dt id="symtable.Class.get_methods">
<tt class="descname">get_methods</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Class.get_methods" title="Permalink to this definition">¶</a></dt>
<dd>Return a tuple containing the names of methods declared in the class.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="symtable.Symbol">
<em class="property">
class </em><tt class="descclassname">symtable.</tt><tt class="descname">Symbol</tt><a class="headerlink" href="#symtable.Symbol" title="Permalink to this definition">¶</a></dt>
<dd><p>An entry in a <a title="symtable.SymbolTable" class="reference internal" href="#symtable.SymbolTable"><tt class="xref docutils literal"><span class="pre">SymbolTable</span></tt></a> corresponding to an identifier in the
source.  The constructor is not public.</p>
<dl class="method">
<dt id="symtable.Symbol.get_name">
<tt class="descname">get_name</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.get_name" title="Permalink to this definition">¶</a></dt>
<dd>Return the symbol&#8217;s name.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.is_referenced">
<tt class="descname">is_referenced</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_referenced" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is used in its block.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.is_imported">
<tt class="descname">is_imported</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_imported" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is created from an import statement.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.is_parameter">
<tt class="descname">is_parameter</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_parameter" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is a parameter.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.is_global">
<tt class="descname">is_global</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_global" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is global.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.is_local">
<tt class="descname">is_local</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_local" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is local to its block.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.is_free">
<tt class="descname">is_free</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_free" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is referenced in its block, but not assigned
to.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.is_assigned">
<tt class="descname">is_assigned</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_assigned" title="Permalink to this definition">¶</a></dt>
<dd>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is assigned to in its block.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.is_namespace">
<tt class="descname">is_namespace</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_namespace" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if name binding introduces new namespace.</p>
<p>If the name is used as the target of a function or class statement, this
will be true.</p>
<p>Note that a single name can be bound to multiple objects.  If the result
is <tt class="xref docutils literal"><span class="pre">True</span></tt>, the name may also be bound to other objects, like an int or
list, that does not introduce a new namespace.</p>
</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.get_namespaces">
<tt class="descname">get_namespaces</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.get_namespaces" title="Permalink to this definition">¶</a></dt>
<dd>Return a list of namespaces bound to this name.</dd></dl>

<dl class="method">
<dt id="symtable.Symbol.get_namespace">
<tt class="descname">get_namespace</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.get_namespace" title="Permalink to this definition">¶</a></dt>
<dd>Return the namespace bound to this name.  If more than one namespace is
bound, a <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</dd></dl>

</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">32.3. <tt class="docutils literal"><span class="pre">symtable</span></tt> &#8212; Access to the compiler&#8217;s symbol tables</a><ul>
<li><a class="reference external" href="#generating-symbol-tables">32.3.1. Generating Symbol Tables</a></li>
<li><a class="reference external" href="#examining-symbol-tables">32.3.2. Examining Symbol Tables</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="ast.html"
                                  title="previous chapter">32.2. Abstract Syntax Trees</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="symbol.html"
                                  title="next chapter">32.4. <tt class="docutils literal docutils literal"><span class="pre">symbol</span></tt> &#8212; Constants used with Python parse trees</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/symtable.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="symbol.html" title="32.4. symbol — Constants used with Python parse trees"
             >next</a> |</li>
        <li class="right" >
          <a href="ast.html" title="32.2. Abstract Syntax Trees"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="language.html" >32. Python Language Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>